package com.jsmy.softapp.activity;

import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jsmy.softapp.MyApp;
import com.jsmy.softapp.R;
import com.jsmy.softapp.aroute.AR;
import com.jsmy.softapp.net.API;
import com.jsmy.softapp.presenter.LoginPresenter;
import com.jsmy.softapp.util.MD5;
import com.jsmy.softapp.util.SPF;
import com.jsmy.softapp.util.ToastUtil;
import com.jsmy.softapp.view.LoginView;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AR.A_LOGIN)
public class LoginActivity extends BaseActivity<LoginView, LoginPresenter> implements LoginView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.te_email)
    TextInputLayout teEmail;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.te_password)
    TextInputLayout tePassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        if (!SPF.getString(this, SPF.SP_ZH, "").isEmpty() && !SPF.getString(this, SPF.SP_MM, "").isEmpty()) {
            presenter.login(SPF.getString(this, SPF.SP_ZH, ""), SPF.getString(this, SPF.SP_MM, ""));
        }
        etEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 10) {
                    //一定要在setError方法之后执行才可
                    teEmail.setError("请输入5-10位账号");
                    teEmail.setErrorEnabled(true);

                } else {
                    //不满足条件需要设置为false
                    teEmail.setErrorEnabled(false);
                }
            }
        });
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 15) {
                    //一定要在setError方法之后执行才可
                    tePassword.setError("请输入6-15位密码");
                    tePassword.setErrorEnabled(true);

                } else {
                    //不满足条件需要设置为false
                    tePassword.setErrorEnabled(false);
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void showLoading() {
        loadingDailog.show();
    }

    @Override
    public void hideLoading() {
        loadingDailog.hide();
    }

    @Override
    public void showLoadingData(String type, Object object) {
        switch (type) {
            case API.LOG_IN:
                ARouter.getInstance().build(AR.A_MAIN).navigation(this, new NavCallback() {
                    @Override
                    public void onArrival(Postcard postcard) {
                        LoginActivity.this.finish();
                    }
                });
                break;
        }

    }

    @Override
    protected void onDestroy() {
        presenter.model.disposable.clear();
        super.onDestroy();
    }

    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        String email = etEmail.getText().toString();
        if (email.isEmpty() || email.length() > 10) {
            ToastUtil.showShort(MyApp.getMyApp(), "账号格式错误，请重新输入");
            return;
        }
        String password = etPassword.getText().toString();
        if (password.isEmpty() || password.length() > 15) {
            ToastUtil.showShort(MyApp.getMyApp(), "密码格式错误，请重新输入");
            return;
        }
        presenter.login(email, MD5.md5(password));
    }

}
